﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace CC.Yacht.Windows.Forms
{
    public partial class LobbyPanel : UserControl
    {
        #region Constructor
        public LobbyPanel()
        {
            InitializeComponent();
        }
        #endregion

        #region Public Events
        public event EventHandler CreatingGame;
        public event EventHandler<JoinGameEventArgs> JoiningGame;
        public event EventHandler<SendMessageEventArgs> SendingMessage;
        #endregion

        #region Public Properties
        public int HorizontalSplitterDistance
        {
            get { return _SplitContainerHorizontal.SplitterDistance; }
            set { _SplitContainerHorizontal.SplitterDistance = value; }
        }

        public Guid LobbyId { get; set; }

        public User User { get; set; }

        public int VerticalSplitterDistance
        {
            get { return _SplitContainerVertical.SplitterDistance; }
            set { _SplitContainerVertical.SplitterDistance = value; }
        }
        #endregion

        #region Private Event Handlers
        private void _ChatPanel_SendingMessage(object sender, SendMessageEventArgs e)
        {
            OnSendingMessage(e);
        }

        private void _GameListBox_CreatingGame(object sender, EventArgs e)
        {
            if (CreatingGame == null)
            {
                throw new InvalidOperationException("Cannot create game when the CreatingGame event is not attached.");
            }

            OnCreatingGame();
        }

        private void _GameListBox_JoiningGame(object sender, JoinGameEventArgs e)
        {
            if (JoiningGame == null)
            {
                throw new InvalidOperationException("Cannot join game when the JoiningGame event is not attached.");
            }

            OnJoiningGame(e);
        }
        #endregion

        #region Protected Methods
        protected void OnCreatingGame()
        {
            if (CreatingGame != null)
            {
                CreatingGame(this, EventArgs.Empty);
            }
        }

        protected void OnJoiningGame(JoinGameEventArgs eventArgs)
        {
            if (JoiningGame != null)
            {
                JoiningGame(this, eventArgs);
            }
        }

        protected void OnSendingMessage(SendMessageEventArgs eventArgs)
        {
            if (SendingMessage != null)
            {
                SendingMessage(this, eventArgs);
            }
        }
        #endregion

        #region Public Methods
        public void GameAdded(Game game)
        {
            _GameListBox.AddGame(game);
        }

        public void GameChanged(Game game)
        {
            _GameListBox.UpdateGame(game);            
        }

        public void GameRemoved(Game game)
        {
            _GameListBox.RemoveGame(game);
        }

        public void MessageReceived(User user, string message)
        {
            _ChatPanel.AddMessage(user, message);
        }

        public void Reset()
        {
            LobbyId = Guid.Empty;

            _ChatPanel.Clear();
            _GameListBox.Clear();
            _UserListBox.Clear();
        }

        public void SetGames(List<Game> games)
        {
            _GameListBox.SetGames(games);
        }

        public void SetUsers(List<User> users)
        {
            _UserListBox.SetUsers(users);    
        }

        public void UserJoined(User user)
        {
            _ChatPanel.AddAction(user.Username + " joined");
            _UserListBox.AddUser(user, true);
        }

        public void UserLeft(User user)
        {
            _ChatPanel.AddAction(user.Username + " left");
            _UserListBox.RemoveUser(user);
        }
        #endregion
    }
}
